package cn.liziguo.tank.factory;

import cn.liziguo.tank.config.GameConfig;
import cn.liziguo.tank.constants.GameEnum;
import cn.liziguo.tank.constants.ItemType;
import cn.liziguo.tank.constants.PropertyConst;
import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.SpawnData;

/**
 * @author Liziguo
 * @date 2022/7/11 14:36
 */
public final class ItemFactory {

    public static void createRandomItem() {
        ItemType itemType = FXGLMath.random(ItemType.values()).orElseThrow();
        SpawnData data = new SpawnData();
        data.put(PropertyConst.ITEM_TYPE, itemType);
        Entity entity = FXGL.entityBuilder(data)
                .type(GameEnum.ITEM)
                .viewWithBBox(itemType.image)
                .collidable()
                .zIndex(20)
                .build();
        final double x = FXGLMath.random(0, GameConfig.WIDTH - entity.getWidth());
        final double y = FXGLMath.random(0, GameConfig.HEIGHT - entity.getHeight());
        entity.setPosition(x, y);
        FXGL.getGameWorld().addEntity(entity);
    }
}
